﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace UI.Forms
{
	public partial class goods_N: FormBase1
	{
        public delegate void DoAction(string classid);//如果有参数的话，定义参数 

        public DoAction FatherRefresh;
        private string ClassID = "";
        private int count = 0;
        public goods_N(string _ClassID)
		{
            InitializeComponent();
            ClassID = _ClassID;
		}

        private void goods_N_Load(object sender, EventArgs e)
        {
            lue_kind.Properties.DataSource = my.bw.GCM_Get("", my.ACID);
            lue_gc.Properties.DataSource = my.bw.GoodsClass_Get("", my.ACID);
            lue_gc.EditValue = ClassID;
            bindunit("");
        }

        private void bindunit(string unitname)
        {
            lue_unit.Properties.DataSource = my.bw.unit_Get("", my.ACID);
            lue_unit.EditValue = unitname;
        }

        private void btn_new_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            vaildbefore();
            if (!dxValidationProvider1.Validate()) return;
            SVC.client.Goods gd = new SVC.client.Goods();
            gd.AveragePrice = decimal.Parse(se_AveragePrice.Text);
            gd.Barcode = te_Barcode.Text;
            gd.ClassID = ClassID;
            gd.Custom1 = te_Custom1.Text;
            gd.Custom2 = te_Custom2.Text;
            gd.Custom3 = te_Custom3.Text;
            gd.Custom4 = te_Custom4.Text;
            gd.Custom5 = te_Custom5.Text;
            gd.isBlankCard = cke_isBlankCard.Checked;
            gd.DealerPrice1 = Convert.ToDecimal(se_DealerPrice1.EditValue);
            gd.DealerPrice2 = Convert.ToDecimal(se_DealerPrice2.EditValue);
            gd.DealerPrice3 = Convert.ToDecimal(se_DealerPrice3.EditValue);
            gd.DealerPrice4 = Convert.ToDecimal(se_DealerPrice4.EditValue);
            gd.DealerPrice5 = Convert.ToDecimal(se_DealerPrice5.EditValue);
            gd.EGoodsName = "";
            gd.GoodsCode = te_GoodsCode.Text;
            gd.GoodsName = te_GoodsName.Text;
            gd.HighPrice = Convert.ToDecimal(se_HighPrice.EditValue);
            gd.inprice = Convert.ToDecimal(se_inprice.EditValue);
            gd.Kind = Convert.ToInt32(lue_kind.EditValue);
            gd.Location = te_Location.Text;
            gd.LowPrice = Convert.ToDecimal(se_LowPrice.EditValue);
            gd.MaxStock = Convert.ToInt32(se_MaxStock.EditValue);
            gd.MinStock = Convert.ToInt32(se_MinStock.EditValue);
            gd.outprice = Convert.ToDecimal(se_outprice.EditValue);
            gd.Remarks = te_remarks.Text;
            gd.Spec = te_spec.Text;
            gd.Unit = lue_unit.Text;
            gd.VipPrice1 = Convert.ToDecimal(se_VipPrice1.EditValue);
            gd.VipPrice2 = Convert.ToDecimal(se_VipPrice2.EditValue);
            gd.VipPrice3 = Convert.ToDecimal(se_VipPrice3.EditValue);
            gd.ZJM = te_ZJM.Text;
            string r = my.bw.Goods_Add(gd, my.ACID);
            if (string.IsNullOrWhiteSpace(r))
            {
                count++;
                resetUI();
            }
            else
            {
                Comm.ShowErorr(r);
            }
        }

        private void resetUI()
        {
            te_Barcode.Text = "";
            te_Custom1.Text = "";
            te_Custom2.Text = "";
            te_Custom3.Text = "";
            te_Custom4.Text = "";
            te_Custom5.Text = "";
            te_GoodsCode.Text = "";
            te_GoodsName.Text = "";
            te_Location.Text = "";
            te_remarks.Text = "";
            te_spec.Text = "";
            te_ZJM.Text = "";
            se_AveragePrice.EditValue = 0;
            se_DealerPrice1.EditValue = 0;
            se_DealerPrice2.EditValue = 0;
            se_DealerPrice3.EditValue = 0;
            se_DealerPrice4.EditValue = 0;
            se_DealerPrice5.EditValue = 0;
            se_HighPrice.EditValue = 0;
            se_inprice.EditValue = 0;
            se_LowPrice.EditValue = 0;
            se_MaxStock.EditValue = 0;
            se_MinStock.EditValue = 0;
            se_outprice.EditValue = 0;
            se_VipPrice1.EditValue = 0;
            se_VipPrice2.EditValue = 0;
            se_VipPrice3.EditValue = 0;
            lue_kind.EditValue = null;
            lue_unit.Text = "";
        }
        private void vaildbefore()
        {
            te_Barcode.Text = te_Barcode.Text.Trim();
            te_Custom1.Text = te_Custom1.Text.Trim();
            te_Custom2.Text = te_Custom2.Text.Trim();
            te_Custom3.Text = te_Custom3.Text.Trim();
            te_Custom4.Text = te_Custom4.Text.Trim();
            te_Custom5.Text = te_Custom5.Text.Trim();
            te_GoodsCode.Text = te_GoodsCode.Text.Trim();
            te_GoodsName.Text = te_GoodsName.Text.Trim();
            te_Location.Text = te_Location.Text.Trim();
            te_remarks.Text = te_remarks.Text.Trim();
            te_spec.Text = te_spec.Text.Trim();
            te_ZJM.Text = te_ZJM.Text.Trim();

        }

        private void goods_N_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (count>0)
            {
                FatherRefresh(ClassID);
            }
        }

        private void btn_newclose_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            vaildbefore();
            if (!dxValidationProvider1.Validate()) return;
            SVC.client.Goods gd = new SVC.client.Goods();
            gd.AveragePrice = decimal.Parse(se_AveragePrice.Text);
            gd.Barcode = te_Barcode.Text;
            gd.ClassID = ClassID;
            gd.Custom1 = te_Custom1.Text;
            gd.Custom2 = te_Custom2.Text;
            gd.Custom3 = te_Custom3.Text;
            gd.Custom4 = te_Custom4.Text;
            gd.Custom5 = te_Custom5.Text;
            gd.isBlankCard = cke_isBlankCard.Checked;
            gd.DealerPrice1 = Convert.ToDecimal(se_DealerPrice1.EditValue);
            gd.DealerPrice2 = Convert.ToDecimal(se_DealerPrice2.EditValue);
            gd.DealerPrice3 = Convert.ToDecimal(se_DealerPrice3.EditValue);
            gd.DealerPrice4 = Convert.ToDecimal(se_DealerPrice4.EditValue);
            gd.DealerPrice5 = Convert.ToDecimal(se_DealerPrice5.EditValue);
            gd.EGoodsName = "";
            gd.GoodsCode = te_GoodsCode.Text;
            gd.GoodsName = te_GoodsName.Text;
            gd.HighPrice = Convert.ToDecimal(se_HighPrice.EditValue);
            gd.inprice = Convert.ToDecimal(se_inprice.EditValue);
            gd.Kind = Convert.ToInt32(lue_kind.EditValue);
            gd.Location = te_Location.Text;
            gd.LowPrice = Convert.ToDecimal(se_LowPrice.EditValue);
            gd.MaxStock = Convert.ToInt32(se_MaxStock.EditValue);
            gd.MinStock = Convert.ToInt32(se_MinStock.EditValue);
            gd.outprice = Convert.ToDecimal(se_outprice.EditValue);
            gd.Remarks = te_remarks.Text;
            gd.Spec = te_spec.Text;
            gd.Unit = lue_unit.Text;
            gd.VipPrice1 = Convert.ToDecimal(se_VipPrice1.EditValue);
            gd.VipPrice2 = Convert.ToDecimal(se_VipPrice2.EditValue);
            gd.VipPrice3 = Convert.ToDecimal(se_VipPrice3.EditValue);
            gd.ZJM = te_ZJM.Text;
            string r = my.bw.Goods_Add(gd, my.ACID);
            if (string.IsNullOrWhiteSpace(r))
            {
                count++;
                this.Close();
            }
            else
            {
                Comm.ShowErorr(r);
            }
        }

        private void lue_unit_Properties_Click(object sender, EventArgs e)
        {
            
        }

        private void lue_unit_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (e.Button.Kind == DevExpress.XtraEditors.Controls.ButtonPredefines.Plus)
            {
                Unit_N frm = new Unit_N();
                frm.FatherRefresh = bindunit;
                frm.ShowDialog();
            }
        }
	}
}